package com.shujia.wyh.day27.network.udpcoding;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

/*
        发送端发送数据，键盘录入数据发送
 */
public class SendDemo2 {
    public static void main(String[] args) throws Exception {
        //创建发送端的Socket对象
        DatagramSocket ds = new DatagramSocket();

        //封装键盘录入的数据
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String line = null;
        while ((line = br.readLine()) != null) {
            if ("886".equals(line)) {
                break;
            }

            //将输入的数据封装成一个数据包
            byte[] bytes = line.getBytes();
            int length = bytes.length;
            InetAddress address = InetAddress.getByName("192.168.10.112");
            int port = 12345;
            DatagramPacket dp = new DatagramPacket(bytes, length, address, port);

            //调用Socket对象发送数据
            ds.send(dp);
        }

        //释放资源
        ds.close();


    }
}
